﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MetroToolbox.Interactivity;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using Windows.ApplicationModel.Core;
using Windows.Foundation;
using Windows.UI.Core;
using Windows.UI.Xaml;

namespace MetroToolbox.Test.Interactivity
{
    class TestAttachableDependencyObject : DependencyObject, IAttachedObject
    {
        public bool AttachCalled { get; protected set; }

        public bool DetachCalled { get; protected set; }

        public DependencyObject AssociatedObject { get; protected set; }

        public void Attach(DependencyObject dependencyObject)
        {
            AttachCalled = true;
            AssociatedObject = dependencyObject;
        }

        public void Detach()
        {
            DetachCalled = true;
            AssociatedObject = null;
        }
    }

    class TestCollection : AttachableCollection<TestAttachableDependencyObject>
    {
    }

    [TestClass]
    public class AttachableCollection
    {
        [TestMethod]
        public async Task AttachableCollection_AddingAnObjectCallsAttach()
        {
            await ExecuteOnUIThreadd(() =>
                {
                    var testCollection = new TestCollection();
                    var testObject = new TestAttachableDependencyObject();
                    testCollection.Add(testObject);

                    Assert.IsTrue(testObject.AttachCalled);
                });
        }

        public IAsyncAction ExecuteOnUIThreadd(DispatchedHandler action)
        {
            return CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, action);
        }
    }
}
